package per.lee.spingboothelper.db.sql;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import per.lee.spingboothelper.db.config.ColumnValueConfig;
import per.lee.spingboothelper.db.model.Column;
import per.lee.spingboothelper.db.model.Table;

public class DataCreater {

	static String INSERT_SQL = "INSERT INTO <<table>>(<<columns>>) VALUES (<<values>>);";

	public static List<String> createBatchInsertSql(Table table) {
		createRamdonValueList(table);
		List<String> sqlList = new ArrayList<String>();
		for (Map<String, String> row : table.getValues()) {
			StringBuffer columns = new StringBuffer();
			StringBuffer values = new StringBuffer();

			for (String column : row.keySet()) {
				columns.append("," + column);
				values.append("," + row.get(column));
			}
			String sql = INSERT_SQL.replaceAll("<<table>>", table.getTableName())
					.replaceAll("<<columns>>", columns.substring(1)).replaceAll("<<values>>", values.substring(1));
			sqlList.add(sql);
		}
		return sqlList;
	}

	private static void createRamdonValueList(Table table) {

		List<Map<String, String>> rows = new ArrayList<Map<String, String>>();

		Random random = new Random();
		
		for (int i = 0; i < 100000; i++) {
			Map<String, String> row = new HashMap<String, String>();
			table.getColumns().forEach(column -> {
				// 如果存在值枚举
				String value = "";
				String columnName = column.getName();
				if (ColumnValueConfig.FIELD_VALUE.containsKey(columnName)) {
					
					int index = random.nextInt(ColumnValueConfig.FIELD_VALUE.get(columnName).size());
					value = wrapValue(ColumnValueConfig.FIELD_VALUE.get(columnName).get(index), column.getType());
				} else {
					value = wrapValue(randomValue(column), column.getType());
				}
				row.put(column.getName(), value);
			});
			overwriteByRule(row,table.getColumns());
			lastWrite(row);
			rows.add(row);
		}
		
		table.setValues(rows);
	}
	
	private static void lastWrite(Map<String, String> row) {
		Random random = new Random();
		row.entrySet().forEach(x->{
			if(x.getValue().equals("")) {
				if(ColumnValueConfig.LSAT_RULE.containsKey(x.getKey())){
					int index = random.nextInt(ColumnValueConfig.LSAT_RULE.get(x.getKey()).size());
					row.put(x.getKey(), ColumnValueConfig.LSAT_RULE.get(x.getKey()).get(index));
				}
			}
		});
		
	}
	
	//根据规则重写
	private static void overwriteByRule(Map<String, String> row,List<Column> columns) {
		System.out.println(row);
		ColumnValueConfig.FIELD_RULE.entrySet().forEach(ruleEntry -> {
			String ruleColumnName = ruleEntry.getKey();
			
			if (row.get(ruleColumnName) != null) {
				
				String newValue = ruleEntry.getValue();
				for (String key : row.keySet()) {
					String value = row.get(key);
					String repalce = "<<" + key + ">>";
					newValue = newValue.replaceAll(repalce, value);
				}
				while(newValue.indexOf("<<") != -1) {
					newValue = newValue.replaceAll(newValue.substring(newValue.indexOf("<<"), newValue.indexOf(">>")+2), "");
				}
				
				String type = columns.stream().filter(x->{
					return x.getName().equals(ruleColumnName);
				}).findAny().get().getType();
				
				String overwriteValue = wrapValue(newValue, type);
				row.put(ruleColumnName, overwriteValue);
			}
		});
	}
	//是否引号包含
	public static String wrapValue(String value, String type) {
		if (type.indexOf("varchar") != -1 || type.indexOf("datetime") != -1 || type.indexOf("char") != -1) {
			return "'" + value + "'";
		}else {
			return value.replaceAll("'", "");
		}

	}

	public static String randomValue(Column c) {
		Random random = new Random();
		if (c.getType().indexOf("int") != -1) {
			return random.nextInt(5) + 1 + "";
		} else if (c.getType().indexOf("bigint") != -1) {
			return random.nextInt(100) + 1 + "";
		} else if (c.getType().indexOf("varchar") != -1) {
			return random.nextInt(100) + 1 + "";
		} else if (c.getType().indexOf("decimal") != -1) {
			return formattedOutputDecimal(random.nextFloat() * 100);
		} else if (c.getType().indexOf("datetime") != -1) {
			return "2019-03-11 22:23:27";
		}else if (c.getType().indexOf("char") != -1) {
			return random.nextInt(100) + 1 + "";
		} 
		System.out.println("null value "+c.getType());
		return "null";
	}

	static DecimalFormat df = new DecimalFormat("######0.00");

	private static String formattedOutputDecimal(double decimal) {
		String result = df.format(decimal);
		return result;
	}
	
	public static void main(String[] args) {
		String string = "<<sss>>12<<hah>>";

		
		while(string.indexOf("<<") != -1) {
	
			string = string.replaceAll(string.substring(string.indexOf("<<"), string.indexOf(">>")+2), "");
		}
		
		System.out.println(string);
	}

}
